package top.hepingan.java.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.hepingan.java.common.Result;
import top.hepingan.java.entity.Activity;
import top.hepingan.java.mapper.ActivityMapper;

@RequiredArgsConstructor
@RequestMapping("/activity")
@RestController
@Slf4j
public class ActivityController {

    private final ActivityMapper activityMapper;
    @GetMapping("/list")
    public Result list(@RequestParam Integer pageNum){
        Page<Activity> p = new Page<>(pageNum,  5);
        LambdaQueryWrapper<Activity> lambdaQueryWrapper = new QueryWrapper<Activity>().lambda()
                .eq(Activity::getStatus, "上线")
                .orderByDesc(Activity::getCreatedTime);
        return Result.data(activityMapper.selectPage(p,lambdaQueryWrapper).getRecords());
    }

    @PostMapping("/add")
    public Result add(@RequestBody Activity activity){
        return activityMapper.insert(activity) > 0 ? Result.ok() : Result.error();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Long id){
        return activityMapper.deleteById(id) > 0 ? Result.ok() : Result.error();
    }



    @GetMapping("/getById")
    public Result getById(@RequestParam Long id){
        return Result.data(activityMapper.selectById(id));
    }
}
